﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using WorkFlowCore.Authorization;
using WorkFlowCore.Common.SimplePluginLoaders;
using WorkFlowCore.Conditions;
using WorkFlowCore.Framework.Authorization;
using WorkFlowCore.Framework.Conditions;
using WorkFlowCore.Framework.EventHandlers;
using WorkFlowCore.Framework.Repositories4EF;
using WorkFlowCore.Framework.UserSelectors;
using WorkFlowCore.IRepositories;
using WorkFlowCore.UserSelectors;

namespace WorkFlowCore.Framework
{
    public static class WorkFlowCoreFrameworkService
    {
        /// <summary>
        /// orm类型，目前暂时只支持本地文件，后续考虑增加 Ef 、sqlsuger 的支持
        /// </summary>
        public enum FrameworkConfigOrmType
        {
            EF,
        }
        public class FrameworkConfig
        {
            public FrameworkConfigOrmType OrmType { get; set; } = FrameworkConfigOrmType.EF;
            
        }
        public static void AddWorkFlowCoreFramework(this IServiceCollection services,Action<FrameworkConfig> config=null)
        {
            var conf = new FrameworkConfig();
            config?.Invoke(conf);
           
            services.AddScoped(typeof(IBasicRepository<,>), typeof(BasicRepository4EF<,>));
            services.AddScoped(typeof(IBasicRepository<>), typeof(BasicRepository4EF<>));
            services.AddScoped(typeof(IWorkStepRepository), typeof(WorkStepRepository4EF));
            services.AddScoped(typeof(IWorkTaskRepository), typeof(WorkTaskRepository4EF));



            services.AddAbpDbContext<WorkflowDbContext>(options =>
            {
                options.AddDefaultRepositories(includeAllEntities: true);
            });



            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            //事件处理
            services.AddScoped<SendTaskEventHandler>();
            services.AddScoped<TaskFinishedEventHandler>();
            services.AddScoped<AutoHandleStepsEventHandler>();

            var assembly = typeof(WorkFlowCoreFrameworkService).Assembly;
            services.Replace(new ServiceDescriptor(typeof(IWorkflowSession),typeof(DefaultSession), ServiceLifetime.Scoped));

            //注册条件和选择器
            #region 注册条件和选择器
            try
            {
                UserSelectorManager.RegisterSelector(services, assembly);
                services.LoadPlugins(Path.Combine(AppContext.BaseDirectory, "Plugins", "UserSelectors"), plugins =>
                {
                    UserSelectorManager.RegisterSelector(services, plugins.Select(p => p.Assembly).ToArray());

                });
                ConditionManager.Registercondition(services, assembly);

                services.LoadPlugins(Path.Combine(AppContext.BaseDirectory, "Plugins", "Conditions"), plugins =>
                {
                    ConditionManager.Registercondition(services, plugins.Select(p => p.Assembly).ToArray());
                });
            }
            catch (Exception ex)
            {
                //
            }
            #endregion

        }
    }
}
